package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Order;
import com.mall.db.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(("/order/oc"))
public class OrderController extends BaseController<Order> {

    @Resource(name = "orderService")
    private OrderService ps;

    @RequestMapping("/paid")
    public String main() {
        return "/views/order/paid";
    }

    @RequestMapping("/received")
    public String login() {
        return "/views/order/received";
    }

    @RequestMapping("/all")
    public String all() {
        return "/views/order/orderAll";
    }

    @RequestMapping("/send")
    public String send() {
        return "/views/order/send";
    }
    @RequestMapping("/sendAdd")
    public String sendAdd() {
        return "/views/order/sendAdd";
    }

    @Override
    public Map<String, Object> find(Order order) {

        PageHelper.startPage(order.getPage(), order.getLimit());

        QueryWrapper<Order> qw = new QueryWrapper<>();

        if(!StringUtils.isEmpty(order.getStatus())){
            qw.eq("status",order.getStatus());
        }

        List<Order> list = ps.list(qw);
        PageInfo<Order> page = new PageInfo<>(list,order.getLimit());

        return this.findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> update(Order order) {
        boolean b = ps.updateById(order);
        return this.iud("操作",b,order);
    }

    @Override
    public Map<String, Object> add(Order order) {
        boolean b = ps.save(order);
        return this.iud("新增",b,order);
    }

    @RequestMapping("/details")
    public ModelAndView details(ModelAndView mav,
                                @RequestParam(value = "id") String id){

        mav.addObject("nid",id);
        mav.setViewName("/views/order/details");
        return mav;
    }

    @Override
    public Map<String, Object> delete(Order order) {
        boolean b = ps.removeById(order);
        return this.iud("删除",b,order);
    }
}
